package com.lzxuni.controller;

import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.lzxuni.po.QxData;
import com.lzxuni.po.QxDataCostom;
import com.lzxuni.po.RhData;
import com.lzxuni.po.RhDataCustom;
import com.lzxuni.po.YongHu;
import com.lzxuni.service.RhDataService;
import com.lzxuni.service.YongHuService;
import com.lzxuni.utils.MethodUtil;


@Controller
@RequestMapping("/rhdata")
public class RhDataController {
	@Autowired
	private RhDataService rhDataService;
	@Autowired
	private YongHuService yongHuService;
	@RequestMapping("/list.html")
	public ModelAndView list(){
		ModelAndView mv = new ModelAndView("/rhdata/list");
		return mv;
	}
	@RequestMapping("/listDo.html")
	public void listDo(RhDataCustom rhDataCustom,HttpServletRequest request, HttpServletResponse response) throws Exception{

		int pageNumber = request.getParameter("pageNumber") == null ? 1 : Integer.parseInt(request.getParameter("pageNumber"));
		int pageSize = request.getParameter("pageSize") == null ? 10 : Integer.parseInt(request
				.getParameter("pageSize"));
		
		// 分页
		@SuppressWarnings("rawtypes")
		PageInfo pageInfo = rhDataService.findRhGroupList(pageNumber, pageSize, rhDataCustom);
		
		@SuppressWarnings("unchecked")
		List<RhData> rhDataList = pageInfo.getList();
		// 返回json数组分页
		JSONObject jsonObject = new JSONObject();
		JSONArray array = new JSONArray();
		
		for (Iterator<RhData> iterator = rhDataList.iterator(); iterator.hasNext();) {
			JSONObject jsonObject1 = new JSONObject();
			RhData rhData = iterator.next();
            //检查是否为userkey创建名称
			YongHu yongHu=yongHuService.findYongHuById(rhData.getUserkey());
			if(yongHu==null){
				YongHu yongHu2=new YongHu();
				yongHu2.setUserkey_id(rhData.getUserkey());
				yongHu2.setCreateTime(MethodUtil.getDate(1, null));
				yongHuService.insertYongHu(yongHu2);
				yongHu=yongHuService.findYongHuById(rhData.getUserkey());
			}
			jsonObject1.put("userkey",rhData.getUserkey() );
			jsonObject1.put("userName", yongHu.getUserName());
			jsonObject1.put("createTime", MethodUtil.formatDate(3,rhData.getCreateTime()));
		

			
			array.add(jsonObject1);
		}
		jsonObject.put("total", pageInfo.getTotal());
		jsonObject.put("rows", array);
		
		// 输出
		MethodUtil.writer(response, jsonObject.toString());
		
	
	}
	// 按userkey查询页面
	@RequestMapping("checkall.html")
	public ModelAndView check() throws Exception{
		ModelAndView mv = new ModelAndView("/rhdata/checkall");
		return mv;
		
	}
	@RequestMapping("/checkallDo.html")
	public void checkallDo(RhDataCustom rhDataCustom,HttpServletRequest request, HttpServletResponse response) throws Exception{

		int pageNumber = request.getParameter("pageNumber") == null ? 1 : Integer.parseInt(request.getParameter("pageNumber"));
		int pageSize = request.getParameter("pageSize") == null ? 10 : Integer.parseInt(request
				.getParameter("pageSize"));
		
		// 分页
		@SuppressWarnings("rawtypes")
		PageInfo pageInfo = rhDataService.findRhDataList(pageNumber, pageSize, rhDataCustom);
		@SuppressWarnings("unchecked")
		List<RhData> rhDataList = pageInfo.getList();
		// 返回json数组分页
		JSONObject jsonObject = new JSONObject();
		JSONArray array = new JSONArray();
		
		for (Iterator<RhData> iterator = rhDataList.iterator(); iterator.hasNext();) {
			JSONObject jsonObject1 = new JSONObject();
			RhData rhData = iterator.next();
			
			jsonObject1.put("id",rhData.getId() );
			jsonObject1.put("userkey",rhData.getUserkey() );
			jsonObject1.put("createTime", MethodUtil.formatDate(0,rhData.getCreateTime()));
			jsonObject1.put("kqwd", rhData.getKqwd());
			jsonObject1.put("kqsd", rhData.getKqsd());
			jsonObject1.put("ph", rhData.getPh());
			jsonObject1.put("shuiwei", rhData.getShuiwei());
			jsonObject1.put("shuiwen", rhData.getShuiwen());
			jsonObject1.put("trsd", rhData.getTrsd());
			jsonObject1.put("trwd", rhData.getTrwd());
			jsonObject1.put("yanfen", rhData.getYanfen());
			
			array.add(jsonObject1);
		}
		jsonObject.put("total", pageInfo.getTotal());
		jsonObject.put("rows", array);
		
		// 输出
		MethodUtil.writer(response, jsonObject.toString());
		
	
	}
}
